package cn.security.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * 自定义身份验证服务类
 */
@Component
public class UserAuthenticationServiceImpl implements UserDetailsService {

    // 注入申明的 PasswordEncoder 密码解析器
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        // 需要返回 UserDetail 实例
        // passwordEncoder.encode("bunana") 会自动进行密码的加密和解析操作
        UserDetails userDetails = new User("xiangjiao",passwordEncoder.encode("bunana"),
                AuthorityUtils.commaSeparatedStringToAuthorityList("add,rm"));
        return userDetails;
    }
}
